<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
com.sun.jersey.guice.spi.container.servlet (jersey-guice 1.9 API)
</TITLE>


<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../../stylesheet.css" TITLE="Style">

<SCRIPT type="text/javascript">
function windowTitle()
{
    if (location.href.indexOf('is-external=true') == -1) {
        parent.document.title="com.sun.jersey.guice.spi.container.servlet (jersey-guice 1.9 API)";
    }
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>

</HEAD>

<BODY BGCOLOR="white" onload="windowTitle();">
<HR>


<!-- ========= START OF TOP NAVBAR ======= -->
<A NAME="navbar_top"><!-- --></A>
<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  <TR ALIGN="center" VALIGN="top">
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <FONT CLASS="NavBarFont1">Class</FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-use.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  </TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>

<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;<A HREF="../../../../../../../com/sun/jersey/guice/spi/container/package-summary.html"><B>PREV PACKAGE</B></A>&nbsp;
&nbsp;NEXT PACKAGE</FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  <A HREF="../../../../../../../index.html?com/sun/jersey/guice/spi/container/servlet/package-summary.html" target="_top"><B>FRAMES</B></A>  &nbsp;
&nbsp;<A HREF="package-summary.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
&nbsp;<SCRIPT type="text/javascript">
  <!--
  if(window==top) {
    document.writeln('<A HREF="../../../../../../../allclasses-noframe.html"><B>All Classes</B></A>');
  }
  //-->
</SCRIPT>
<NOSCRIPT>
  <A HREF="../../../../../../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>


</FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A>
<!-- ========= END OF TOP NAVBAR ========= -->

<HR>
<H2>
Package com.sun.jersey.guice.spi.container.servlet
</H2>
Provides support for Guice-based Web applications.
<P>
<B>See:</B>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="#package_description"><B>Description</B></A>
<P>

<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Class Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../../../../../com/sun/jersey/guice/spi/container/servlet/GuiceContainer.html" title="class in com.sun.jersey.guice.spi.container.servlet">GuiceContainer</A></B></TD>
<TD>A <CODE>Servlet</CODE> or <CODE>Filter</CODE> for deploying root resource classes
 with Guice integration.</TD>
</TR>
</TABLE>
&nbsp;

<P>
<A NAME="package_description"><!-- --></A><H2>
Package com.sun.jersey.guice.spi.container.servlet Description
</H2>

<P>
Provides support for Guice-based Web applications.
 <p>
 Guice support is enabled by referencing the Guice filter
 <A HREF="http://google-guice.googlecode.com/svn/tags/3.0/javadoc/com/google/inject/servlet/GuiceFilter.html?is-external=true" title="class or interface in com.google.inject.servlet"><CODE>GuiceFilter</CODE></A> and an application
 specific <CODE>ServletContextListener</CODE> that extends from
 <A HREF="http://google-guice.googlecode.com/svn/tags/3.0/javadoc/com/google/inject/servlet/GuiceServletContextListener.html?is-external=true" title="class or interface in com.google.inject.servlet"><CODE>GuiceServletContextListener</CODE></A> in the web.xml.
 For example, the web.xml may be as follows:
 <blockquote><pre>
   &lt;web-app&gt;
     &lt;listener&gt;
       &lt;listener-class&gt;foo.MyGuiceConfig&lt;/listener-class&gt;
     &lt;/listener&gt;
     &lt;filter&gt;
       &lt;filter-name&gt;Guice Filter&lt;/filter-name&gt;
       &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
     &lt;/filter&gt;
     &lt;filter-mapping&gt;
       &lt;filter-name>Guice Filter&lt;/filter-name&gt;
       &lt;url-pattern>/*&lt;/url-pattern&gt;
     &lt;/filter-mapping&gt;
   &lt;/web-app&gt;
 </blockquote></pre>
 and the application specific servlet context listener may be as follows:
 <blockquote><pre>
     package foo;
 
     import com.google.inject.Guice;
     import com.google.inject.Injector;
     import com.google.inject.servlet.GuiceServletContextListener;
     import com.google.inject.servlet.ServletModule;
     import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
     import com.sun.jersey.guice.JerseyServletModule;
     import foo.GuiceResource;
     
     public class MyGuiceConfig extends GuiceServletContextListener {
 
         &#64;Override
         protected Injector getInjector() {
             return Guice.createInjector(new JerseyServletModule() {
 
                 &#64;Override
                 protected void configureServlets() {
                     bind(GuiceResource.class);
 
                     serve("/*").with(GuiceContainer.class);
                 }
         });
     } 
 }
 </blockquote></pre>
 Notice that one class <code>GuiceResource</code> is bound and the
 <A HREF="../../../../../../../com/sun/jersey/guice/spi/container/servlet/GuiceContainer.html" title="class in com.sun.jersey.guice.spi.container.servlet"><CODE>GuiceContainer</CODE></A> is 
 declared in the <code>serve</code> method. A instance of 
 module <A HREF="../../../../../../../com/sun/jersey/guice/JerseyServletModule.html" title="class in com.sun.jersey.guice"><CODE>JerseyServletModule</CODE></A> is created. This
 module extends from <A HREF="http://google-guice.googlecode.com/svn/tags/3.0/javadoc/com/google/inject/servlet/ServletModule.html?is-external=true" title="class or interface in com.google.inject.servlet"><CODE>ServletModule</CODE></A> and
 provides JAX-RS and Jersey bindings.
 <p>
 Instances of
 <code>GuiceResource</code> will be managed according to the scope declared
 using Guice defined scopes. For example the <code>GuiceResource</code>
 could be as follows:
 <blockquote><pre>
    package foo;
 
    import javax.ws.rs.GET;
    import javax.ws.rs.Produces;
    import javax.ws.rs.Path;
    import javax.ws.rs.QueryParam;
    import javax.enterprise.context.RequestScoped;
 
    &#64;Path("bound/perrequest")
    &#64;RequestScoped
    public class GuiceResource {
 
        &#64;QueryParam("x") String x;
 
        &#64;GET
        &#64;Produces("text/plain")
        public String getIt() {
            return "Hello From Guice: " + x;
        }     
    }
 </blockquote></pre>
 <p>
 Any root resource or provider classes bound by Guice
 will be automatically registered. It is possible to intermix Guice and
 non-Guice registration of classes by additionally using the normal
 Jersey-based registration mechanisms in the servlet context listener
 implementation. For example:
 <blockquote><pre>
     package foo;

     import com.google.inject.Guice;
     import com.google.inject.Injector;
     import com.google.inject.servlet.GuiceServletContextListener;
     import com.google.inject.servlet.ServletModule;
     import com.sun.jersey.api.core.PackagesResourceConfig;
     import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
     import com.sun.jersey.guice.JerseyServletModule;
     import foo.GuiceResource;
     import java.util.HashMap;
     import java.util.Map; 
 
     public class GuiceServletConfig extends GuiceServletContextListener {
 
         &#64;Override
         protected Injector getInjector() {
             return Guice.createInjector(new JerseyServletModule() {
 
                 &#64;Override
                 protected void configureServlets() {
                     bind(GuiceResource.class);

                     Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
                     params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "unbound");
                     serve("/*").with(GuiceContainer.class, params);
                 }
             });
         }
     }
 </blockquote></pre>
 <p>
 Any root resource or provider classes found in the package <code>unbound</code>
 or sub-packages of will be registered whether they be Guice-bound nor not.
 <p>
 Sometimes it is convenient for developers not to explicitly bind a
 resource or provider, let Guice instantiate, and let Jersey manage
 the life-cycle. This behavior can be enabled for a resource or
 provider class as follows:
 <ol>
 <li>a class constructor is annotated with <A HREF="http://google-guice.googlecode.com/svn/tags/3.0/javadoc/com/google/inject/Inject.html?is-external=true" title="class or interface in com.google.inject"><CODE>Inject</CODE></A>;
 <li>the class is not explicitly bound in Guice; and
 <li>the class is registered using a Jersey based registration mechanism,
     for example using package scanning registration.
 </ol>
 <p>
 In other cases it is convenient to let Jersey instantiate and manage
 the life-cycle and let Guice perform injection. This behavior can be
 enabled for a resource or provider class as follows:
 <ol>
 <li>a field or method is annotated with <A HREF="http://google-guice.googlecode.com/svn/tags/3.0/javadoc/com/google/inject/Inject.html?is-external=true" title="class or interface in com.google.inject"><CODE>Inject</CODE></A>;
 <li>the class is not explicitly bound in Guice; and
 <li>the class is registered using a Jersey based registration mechanism,
     for example using package scanning registration.
 </ol>
<P>

<P>
<DL>
</DL>
<HR>


<!-- ======= START OF BOTTOM NAVBAR ====== -->
<A NAME="navbar_bottom"><!-- --></A>
<A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_bottom_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  <TR ALIGN="center" VALIGN="top">
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <FONT CLASS="NavBarFont1">Class</FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-use.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  </TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>

<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;<A HREF="../../../../../../../com/sun/jersey/guice/spi/container/package-summary.html"><B>PREV PACKAGE</B></A>&nbsp;
&nbsp;NEXT PACKAGE</FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  <A HREF="../../../../../../../index.html?com/sun/jersey/guice/spi/container/servlet/package-summary.html" target="_top"><B>FRAMES</B></A>  &nbsp;
&nbsp;<A HREF="package-summary.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
&nbsp;<SCRIPT type="text/javascript">
  <!--
  if(window==top) {
    document.writeln('<A HREF="../../../../../../../allclasses-noframe.html"><B>All Classes</B></A>');
  }
  //-->
</SCRIPT>
<NOSCRIPT>
  <A HREF="../../../../../../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>


</FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>
<!-- ======== END OF BOTTOM NAVBAR ======= -->

<HR>
Copyright &#169; 2011 <a href="http://www.oracle.com/">Oracle Corporation</a>. All Rights Reserved.
</BODY>
</HTML>
